package org.example.mybatisdemo.mapper;

import org.apache.ibatis.annotations.*;
import org.example.mybatisdemo.model.UserInfo;

import java.util.List;

@Mapper
public interface UserInfoMapper {
    //查询所有用户信息
    @Select("select * from user")
    List<UserInfo> selectAll();

    @Select("select id, username, password, create_time, update_time from user where id= #{aa}")
    UserInfo selectUserById(Integer id);

    // 非唯一主键查询,返回结果建议使用List
    @Select("select * from user where id= #{id} and username= #{username}")
    List<UserInfo> selectUserByIdAndUsername(@Param("id") Integer id, @Param("username") String username);

    //传递对象
    @Insert("insert into user (username, password)" + "values (#{username}, #{password})")
    Integer insertUser(UserInfo userInfo);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into user (username, password) " +
            "values (#{userInfo.username}, #{userInfo.password})")
    Integer insertUserByParam(@Param("userInfo") UserInfo userInfo);

    //删除数据
    @Delete("delete from user where id =#{id}")
    Integer delete(Integer id);

    //修改数据
    @Update("update user set password = #{password} where id =#{id}")
    Integer update(Integer id, String password);

    //# 和 $ 区别
    @Select("select * from user where id= ${id}")
    UserInfo selectUserById1(Integer id);


    //    @Select("select * from user_info where username= '${name}'")
    @Select("select * from user where username= '${name}' and password = '${password}'")
    List<UserInfo> selectUserByName(String name, String password);

    // 排序功能
    @Select("select * from user order by id ${order}")
    List<UserInfo> selectUserByOrder(String order);

    @Select("select * from user where username like CONCAT('%',#{name},'%')")
    List<UserInfo> selectUserByLike(String name);

    @Insert("<script>" +
            "insert into user" +
            " <trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">" +
            " <if test=\"username!=null\"> username ,</if>" +
            " <if test=\"password!=null\"> password ,</if>" +
            " </trim> values" +
            " <trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">" +
            " <if test=\"username!=null\"> #{username},</if>" +
            " <if test=\"password!=null\"> #{password},</if>" +
            " </trim>"+
            "</script>")
    Integer insertUserByCondition(UserInfo userInfo);
}
